/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet.edt;

import bdd.DBLink;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import src.IntervalCreneau;

/**
 *
 * @author boomar
 */
public class AjoutCrenau extends HttpServlet {

    public static final String DELETE_CRENAU = "DELETE FROM CRENEAU WHERE NUMSALLE = ? AND NUMENSEIGNANT = ? AND NUMTYPE = ? AND NUMEC = ? AND HORAIREDEBUT = ? AND HORAIREFIN = ? AND IDDATE = ? AND NUMPROMOTION = ?";
    public static final String INSERT_CRENAU = "INSERT INTO CRENEAU (NUMSALLE, NUMENSEIGNANT, NUMTYPE , NUMEC, HORAIREDEBUT, HORAIREFIN, IDDATE, NUMPROMOTION) VALUES (?,?,?,?,?,?,?,?)";
    public static final String SELECT_CRENAU = "SELECT * FROM CRENEAU WHERE NUMSALLE = ? AND NUMENSEIGNANT = ? AND NUMTYPE = ? AND NUMEC = ? AND HORAIREDEBUT = ? AND HORAIREFIN = ? AND IDDATE = ? AND NUMPROMOTION = ?";
    public static final String SELECT_CRENEAU_BIS = "SELECT * FROM CRENEAU WHERE IDDATE = ? AND NUMPROMOTION = ?";
    public static final String INSERT_JOUR = "INSERT INTO JOURS (IDDATE) VALUES (?)";
    public static final String SELECT_JOUR = "SELECT IDDATE FROM JOURS WHERE IDDATE = ?";
    public String ec, type, jour, salle, enseignant, minuteDebut, minuteFin, dateDebut, dateFin, selectWeekDay;
    public int heureDebut, heureFin, promotion;
    public boolean isInterval;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            addNewCrenau(out, request);
        } finally {
            out.close();
        }
    }

    protected void addNewCrenau(PrintWriter out, HttpServletRequest request) {
        try {
            this.ec = (request.getParameter("ec") == null) ? "" : request.getParameter("ec");
            this.type = (request.getParameter("type") == null) ? "" : request.getParameter("type");
            this.jour = (request.getParameter("jour") == null) ? "" : request.getParameter("jour");
            this.salle = (request.getParameter("salle") == null) ? "" : request.getParameter("salle");
            this.enseignant = (request.getParameter("enseignant") == null) ? "" : request.getParameter("enseignant");
            this.heureDebut = (request.getParameter("heureDebut") == null || request.getParameter("heureDebut").equals("")) ? 0 : Integer.parseInt(request.getParameter("heureDebut"));
            this.minuteDebut = (request.getParameter("minuteDebut") == null || request.getParameter("minuteDebut").equals("")) ? "00" : request.getParameter("minuteDebut");
            this.heureFin = (request.getParameter("heureFin") == null || request.getParameter("heureFin").equals("")) ? 0 : Integer.parseInt(request.getParameter("heureFin"));
            this.minuteFin = (request.getParameter("minuteFin") == null || request.getParameter("minuteFin").equals("")) ? "00" : request.getParameter("minuteFin");
            this.promotion = (request.getParameter("promotion") == null || request.getParameter("promotion").equals("")) ? 0 : Integer.parseInt(request.getParameter("promotion"));
            this.isInterval = (request.getParameter("minuteFin") == null) ? false : Boolean.parseBoolean(request.getParameter("isInterval"));
            this.dateDebut = (request.getParameter("dateDebut") == null || request.getParameter("dateDebut").equals("")) ? "00" : request.getParameter("dateDebut");
            this.dateFin = (request.getParameter("dateFin") == null || request.getParameter("dateFin").equals("")) ? "00" : request.getParameter("dateFin");
            this.selectWeekDay = (request.getParameter("selectWeekDay") == null) ? "" : request.getParameter("selectWeekDay");

            if (ec.equals("")
                    || type.equals("")
                    || (jour.equals("") && (dateDebut.equals("") && dateFin.equals("") && selectWeekDay.equals("")))
                    || salle.equals("")
                    || enseignant.equals("")
                    || (heureDebut == 0 && minuteDebut.equals("00"))
                    || (heureFin == 0 && minuteFin.equals("00"))
                    || promotion == 0) {
                out.println("<p> <font color=\"red\"> Impossible de soummetre la demande \n Tous les champs sont obligatoires  </font> </p>");
                return;
            }

            if (((heureDebut * 60) + Integer.parseInt(minuteDebut)) >= ((heureFin * 60) + Integer.parseInt(minuteFin))) {
                out.println("<p> <font color=\"red\"> Impossible de soummetre la demande \n L'heure du début ne peut être suppérieure à celle de la fin </font> </p>");
                return;
            }

            if (isInterval == true) {
                String[] sDateDebut = dateDebut.split("/");
                String[] sDateFin = dateFin.split("/");

                Date oDateDebut = new Date(Integer.parseInt(sDateDebut[2]), Integer.parseInt(sDateDebut[1]), Integer.parseInt(sDateDebut[0]));
                Date oDateFin = new Date(Integer.parseInt(sDateFin[2]), Integer.parseInt(sDateFin[1]), Integer.parseInt(sDateFin[0]));

                if (oDateDebut.after(oDateFin)) {
                    out.println("<p> <font color=\"red\"> Impossible de soummetre la demande \n La date du début ne peut être suppérieure à celle de la fin </font> </p>");
                    return;
                }

                SimpleDateFormat formatterDate = new SimpleDateFormat("dd/MM/yy");
                String tmpJour;
                for (Date leJour : getDaysInRange(oDateDebut, oDateFin, selectWeekDay)) {
                    tmpJour = formatterDate.format(leJour)+" 00:00:00,000000000";
                    insertCreneau(tmpJour, out);
                }
            } else {
                jour += " 00:00:00,000000000";
                insertCreneau(jour, out);
            }
        } catch (Exception e) {
            e.printStackTrace(out);
            out.println("<p> <font color=\"red\"> Suite à un problèmem technique, vote demande ne peut être traitée </font> </p>");
            out.println("<p> Détails : " + e.getMessage() + "</p>");
        }
    }

    public void insertCreneau(String leJour, PrintWriter out) throws SQLException {
        ArrayList<IntervalCreneau> listCreneau = getCreneauJour(leJour, promotion, out);
        IntervalCreneau intervalCreneau = new IntervalCreneau(Integer.parseInt(heureDebut + minuteDebut), Integer.parseInt(heureFin + minuteFin));

        if (intervalCreneau.intersects(listCreneau)) {
            out.println("<p> <font color=\"red\"> Attention : Un chevauchement entre deux créneaux a été detecté pour le : " + leJour.substring(0, 8) + " </font> </p>");
            return;
        }

        DBLink dbLink = new DBLink();
        dbLink.connexion();

        dbLink.setPst(dbLink.getConnection().prepareStatement(SELECT_JOUR));
        dbLink.getPst().setString(1, leJour);
        dbLink.setRs(dbLink.getPst().executeQuery());

        if (dbLink.getRs().next() == false) {
            //jour = dbLink.getRs().getString("IDDATE");
            dbLink.setPst(dbLink.getConnection().prepareStatement(INSERT_JOUR));
            dbLink.getPst().setString(1, leJour);
            dbLink.getPst().executeQuery();
        }

        dbLink.setPst(dbLink.getConnection().prepareStatement(SELECT_CRENAU));
        dbLink.getPst().setString(1, salle);
        dbLink.getPst().setString(2, enseignant);
        dbLink.getPst().setString(3, type);
        dbLink.getPst().setString(4, ec);
        dbLink.getPst().setString(5, heureDebut + "" + minuteDebut);
        dbLink.getPst().setString(6, heureFin + "" + minuteFin);
        dbLink.getPst().setString(7, leJour);
        dbLink.getPst().setString(8, Integer.toString(promotion));
        dbLink.setRs(dbLink.getPst().executeQuery());



        if (dbLink.getRs().next() == false) {
            dbLink.setPst(dbLink.getConnection().prepareStatement(INSERT_CRENAU));
            dbLink.getPst().setString(1, salle);
            dbLink.getPst().setString(2, enseignant);
            dbLink.getPst().setString(3, type);
            dbLink.getPst().setString(4, ec);
            dbLink.getPst().setString(5, heureDebut + "" + minuteDebut);
            dbLink.getPst().setString(6, heureFin + "" + minuteFin);
            dbLink.getPst().setString(7, leJour);
            dbLink.getPst().setString(8, Integer.toString(promotion));
            dbLink.getPst().executeQuery();


        } else {
            out.println("<p> Le crénau existe déjà pour le jour :" + leJour.substring(0, 8) + " </p>");
            return;
        }

        out.println("<p> <font color=\"green\"> Le nouveau crénau a bien été enregistré pour le " + leJour.substring(0, 8) + "</font> </p>");
    }

    public ArrayList<IntervalCreneau> getCreneauJour(String leJour, int numPromotion, PrintWriter out) {

        try {
            ArrayList<IntervalCreneau> listCreneau = new ArrayList<IntervalCreneau>();
            DBLink dbLink = new DBLink();
            dbLink.connexion();

            dbLink.setPst(dbLink.getConnection().prepareStatement(SELECT_CRENEAU_BIS));
            dbLink.getPst().setString(1, leJour);
            dbLink.getPst().setString(2, Integer.toString(numPromotion));
            dbLink.setRs(dbLink.getPst().executeQuery());

            while (dbLink.getRs().next()) {
                listCreneau.add(new IntervalCreneau(dbLink.getRs().getInt("HORAIREDEBUT"),
                        dbLink.getRs().getInt("HORAIREFIN")));
                //out.println("<p>HORAIREDEBUT : " + dbLink.getRs().getInt("HORAIREDEBUT") + "\n HORAIREFIN : " + dbLink.getRs().getInt("HORAIREFIN")+"</p>");
            }
            return listCreneau;
        } catch (Exception e) {
            e.printStackTrace(out);
            out.println("<p> <font color=\"red\"> Suite à un problème technique, vote demande ne peut être traitée </font> </p>");
            out.println("<p> Détails : " + e.getMessage() + "</p>");
            return null;
        }
    }

    public ArrayList<Date> getDaysInRange(Date strdate, Date enddate, String dayName) throws ParseException {

        ArrayList<Date> dates = new ArrayList<Date>();

        SimpleDateFormat formatterDay = new SimpleDateFormat("EEEE");

        Calendar ss = Calendar.getInstance();
        Calendar ee = Calendar.getInstance();

        ss.setTime(strdate);
        ee.setTime(enddate);

        ee.add(Calendar.DATE, 1);

        String day = "";

        while (!ss.equals(ee)) {
            day = formatterDay.format(ss.getTime());
            if (day.equals(dayName)) {
                dates.add(ss.getTime());
            }
            ss.add(Calendar.DATE, 1);
        }
        return dates;
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);




    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);




    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";


    }// </editor-fold>
}
